<?php

namespace App\Http\Controllers;

use App\Models\Doctor;
use App\Service\Push;
use App\User;
use App\Util\Tool;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Service\PushMessage;

/**
 * Class PushMessageController
 * @package App\Http\Controllers
 * 腾讯推送
 */
class PushMessageController extends BaseController
{

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 多用户推送
     */

    public function pushManyUsers(Request $request)
    {
        $param = $request->input();
        $users_id = Tool::handelString($param['user_id']);
        if (empty($users_id)) {
            return $this->ResultJson(0, '用户id不能为空!', []);
        }
        if (empty($param['title'])) {
            return $this->ResultJson(0, '标题不能为空!', []);
        }
        if (empty($param['content'])) {
            return $this->ResultJson(0, '内容不能为空!', []);
        }
        $usersInfo = $this->getUsersTpnsToken($users_id);
        $android = [];
        $ios = [];
        foreach ($usersInfo as $k => $v) {
            if ($v->use_phone_type == 1) {
                $android[] = $v->tpns_token;
            } elseif ($v->use_phone_type == 2) {
                $ios[] = $v->tpns_token;
            }
        }
        $push = new Push();
        if (!empty($android)) {
            $res = $push->sendToAndroid($android, $param['title'], $param['content'], [],1);
        }
        if (!empty($ios)) {
            $res = $push->sendToIos($ios, $param['title'], $param['content'], [],1);
        }
        if (!empty($res['push_id'])) {
            return $this->ResultJson(1, '推送成功!', []);
        }
        return $this->ResultJson(0, '推送失败!', []);
    }




    public function pushUser(Request $request)
    {
        $param = $request->input();
        if ($param['type'] == 1) { //用户
            $user = (new User())->where([['id', '=', $param['user_id']]])->first();
        } elseif ($param['type'] == 2) { //医生
            $user = (new Doctor())->where([['id', '=', $param['user_id']]])->first();
        }
        $push = new Push();
        if ($user->use_phone_type == 1) { //android
            if ($user->tpns_token) {
                $dd['title'] = $param['title'];
                $dd['content'] = $param['content'];
                $dd['token'] = $user->phone_token;
                $res = $push->sendToAndroid($user, $param['title'], $param['content'], []);
//                $res = PushMessage::androidSingleEquipmentPush($dd);
            }
        } else { //ios
            if ($user->tpns_token) {
                $dd['title'] = $param['title'];
                $dd['content'] = $param['content'];
                $dd['token'] = $user->phone_token;
                $res = $push->sendToIos($user, $param['title'], $param['content'], []);
//                $res = PushMessage::androidSingleEquipmentPush($dd);
//                dd($res);
            }
        }
        if (!empty($res['push_id'])) {
            return $this->ResultJson(1, '推送成功!', []);
        }

        return $this->ResultJson(0, '文件上传失败!', []);
    }

    /**
     * @param $users
     * @return mixed
     * 获取用户tpns
     */
    public function getUsersTpnsToken($users)
    {
        $w[] = [function ($query) use ($users) {
            return $query->whereIn('id', $users);
        }];
        return (new User())->where($w)->get();
    }
}
